package com.logAndSignConsummer.controller;

import com.alibaba.fastjson.JSON;
import com.logAndSignConsummer.service.StoreServiceFeign;
import entity.ResponseMessage;
import entity.Store;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import util.DownLoadFile;
import util.FileUpload;

import javax.servlet.http.HttpServletResponse;

/**
 * The type Store controller.
 */
@RestController
@RequestMapping("/store")
@CrossOrigin
public class StoreController {
    @Autowired
    private StoreServiceFeign storeServiceFeign;

    /**
     * Select all string.
     *
     * @param store the store
     * @return the string
     */
    @RequestMapping("/selectAll")
    public String selectAll(@RequestBody Store store) {
        String s = storeServiceFeign.selectAll(store);
        return s;
    }

    /**
     * Insert store string.
     *
     * @param store the store
     * @return the string
     */
    @RequestMapping("/insertStore")
    public String insertStore(@RequestBody Store store) {
        String insert = storeServiceFeign.insert(store);
        return insert;
    }

    /**
     * Update store string.
     *
     * @param store the store
     * @return the string
     */
    @RequestMapping("/updateStore")
    public String updateStore(@RequestBody Store store) {
        String update = storeServiceFeign.update(store);
        return update;
    }

    /**
     * Delete string.
     *
     * @param id the id
     * @return the string
     */
    @RequestMapping("/deleteStore/{id}")
    public String delete(@PathVariable("id") String id) {
        String delete = storeServiceFeign.delete(id);
        return delete;
    }

    /**
     * Upload string.
     *
     * @param multipartFile the multipart file
     * @return the string
     */
    @RequestMapping("/upload")
    public String upload(@RequestParam(name = "file") MultipartFile multipartFile) {
        ResponseMessage msg = FileUpload.fileUpload(multipartFile);
        return JSON.toJSONString(msg);
    }

    @RequestMapping("/download/{fileName}")
    public String download(@PathVariable("fileName") String fileName, HttpServletResponse response) {
        ResponseMessage msg = DownLoadFile.downLoadFile(response, fileName);
        return JSON.toJSONString(msg);
    }
}
